package com.smart.gui;

import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import org.jdom2.Element;

import com.smart.core.AppManager;
import com.smart.core.SMART_APP;

public class ButtonHandler implements ActionListener {
	javax.swing.JFrame frame;

	private AppManager am;

	public final static String BD = "Business Drivers";
	public final static String GAEOF = "Goal and Expectations of Migration";
	public final static String BAS = "Budget and Schedule";
	public final static String OE = "Other Efforts";

	public final static String TF = "Technical Factors";
	public final static String LATTEQ = "Legacy and Target Test Environment Questionnaire";
	public final static String HLUOTLTE = "High Level Understanding of the Legacy Test Environment";
	public final static String CTCFM = "Candidate Test Cases for Migration";
	public final static String HLUOFTTE = "High Level Understanding of the Target Environment";

	public final static String DCTCFMQ = "Define Candidate Test Cases for Migration Questionnaire";
	public final static String COTC = "Characteristics of Test Cases";
	public final static String TCC = "Test Case Creation";
	public final static String TCA = "Test Code Analysis";

	public final static String DETEQ = "Describe Existing Test Enviroment Questionnaire";
	public final static String F = "Functionality";
	public final static String H = "History";
	public final static String P = "Platform";
	public final static String TD = "Test Documentation";
	public final static String DE = "Development Environment";
	public final static String A = "Architecture";
	public final static String C = "Code";

	public final static String DTTEQ = "Describe Target Test Environment Questionnaire";
	public final static String CONSTRAINTS = "Constraints";
	public final static String OWNERSHIP = "Ownership";
	public final static String TEP = "Test Execution Platform";
	public final static String CI = "Cloud Infrastructure";

	public final static String OR = "Operational Results";
	public final static String SHQ = "Stakeholders Questionaire";
	public final static String LTEEU = "Legacy Test Environment End Users";
	public final static String LSO = "Legacy System Owner";
	public final static String LSDAM = "Legacy System Developers and Maintainers";
	public final static String PCTM = "People Conducting the Migration";
	public final static String TEO = "Target Envirnoment Owners";

	public final static String SQ = "Start Questionnaire";
	public final static String AR = "Analyze Results";
	public final static String PS = "Print Summary";
	public final static String SAVE = "save";
	public final static String EXIT = "exit";
	public final static String ABOUT = "about";

	public ButtonHandler(javax.swing.JFrame f) {
		frame = f;
	}

	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		am = SMART_APP.get_am();
		TOC_Form_Business_Drivers toc_f_bd = am.get_toc_f_bd();
		Business_Drivers_Part1 bd_p1 = am.get_bdp1();
		Business_Drivers_Part2 bd_p2 = am.get_bdp2();
		Business_Drivers_Part3 bd_p3 = am.get_bdp3();
		TOC_Form_Technical_Factors toc_f_tf = am.get_toc_f_tf();
		TOC_Form_Legacy_and_Target_Test_Environment toc_f_latte = am
				.get_toc_f_latte();
		Legacy_and_Target_Test_Environment_Part1 latte_part1 = am.get_lattep1();
		Legacy_and_Target_Test_Environment_Part2 latte_part2 = am.get_lattep2();
		Legacy_and_Target_Test_Environment_Part3 latte_part3 = am.get_lattep3();
		TOC_Form_Define_Candidate_Test_Cases toc_f_dctc = am.get_toc_f_dctc();
		Define_Candidate_Test_Cases_Part1 cotc_part1 = am.get_dctcp1();
		Define_Candidate_Test_Cases_Part2 cotc_part2 = am.get_dctcp2();
		Define_Candidate_Test_Cases_Part3 cotc_part3 = am.get_dctcp3();
		TOC_Form_Describe_Existing_Test_Environment toc_f_dete = am
				.get_toc_f_dete();
		Describe_Existing_Test_Environment_Part1 dete_part1 = am.get_detep1();
		Describe_Existing_Test_Environment_Part2 dete_part2 = am.get_detep2();
		Describe_Existing_Test_Environment_Part3 dete_part3 = am.get_detep3();
		Describe_Existing_Test_Environment_Part4 dete_part4 = am.get_detep4();
		Describe_Existing_Test_Environment_Part5 dete_part5 = am.get_detep5();
		Describe_Existing_Test_Environment_Part6 dete_part6 = am.get_detep6();
		Describe_Existing_Test_Environment_Part7 dete_part7 = am.get_detep7();
		TOC_Form_Describe_Target_Test_Environment toc_f_dtte = am
				.get_toc_f_dtte();
		Describe_Target_Test_Environment_Part1 dtte_part1 = am.get_dttep1();
		Describe_Target_Test_Environment_Part2 dtte_part2 = am.get_dttep2();
		Describe_Target_Test_Environment_Part3 dtte_part3 = am.get_dttep3();
		Describe_Target_Test_Environment_Part4 dtte_part4 = am.get_dttep4();
		TOC_Form_Operational_Results toc_f_or = am.get_toc_f_or();
		TOC_Form_Stakeholders toc_f_shq = am.get_toc_f_s();
		Stakeholders_Part1 sh_part1 = am.get_sp1();
		Stakeholders_Part2 sh_part2 = am.get_sp2();
		Stakeholders_Part3 sh_part3 = am.get_sp3();
		Stakeholders_Part4 sh_part4 = am.get_sp4();
		Stakeholders_Part5 sh_part5 = am.get_sp5();
		About_Frame af = am.get_af();

		if (command == BD) {
			toc_f_bd.setVisible(true);
		}

		if (command == GAEOF) {
			bd_p1.setVisible(true);
		}

		if (command == BAS) {
			bd_p2.setVisible(true);
		}

		if (command == OE) {
			bd_p3.setVisible(true);
		}

		if (command == TF) {
			toc_f_tf.setVisible(true);
		}

		if (command == LATTEQ) {
			toc_f_latte.setVisible(true);
		}

		if (command == HLUOTLTE) {
			latte_part1.setVisible(true);
		}

		if (command == CTCFM) {
			latte_part2.setVisible(true);
		}

		if (command == HLUOFTTE) {
			latte_part3.setVisible(true);
		}

		if (command == DCTCFMQ) {
			toc_f_dctc.setVisible(true);
		}

		if (command == COTC) {
			cotc_part1.setVisible(true);
		}

		if (command == TCC) {
			cotc_part2.setVisible(true);
		}

		if (command == TCA) {
			cotc_part3.setVisible(true);
		}

		if (command == DETEQ) {
			toc_f_dete.setVisible(true);
		}

		if (command == F) {
			dete_part1.setVisible(true);
		}

		if (command == H) {
			dete_part2.setVisible(true);
		}

		if (command == P) {
			dete_part3.setVisible(true);
		}

		if (command == TD) {
			dete_part4.setVisible(true);
		}

		if (command == DE) {
			dete_part5.setVisible(true);
		}

		if (command == A) {
			dete_part6.setVisible(true);
		}

		if (command == C) {
			dete_part7.setVisible(true);
		}

		if (command == DTTEQ) {
			toc_f_dtte.setVisible(true);
		}

		if (command == CONSTRAINTS) {
			dtte_part1.setVisible(true);
		}

		if (command == OWNERSHIP) {
			dtte_part2.setVisible(true);
		}

		if (command == TEP) {
			dtte_part3.setVisible(true);
		}

		if (command == CI) {
			dtte_part4.setVisible(true);
		}

		if (command == OR) {
			toc_f_or.setVisible(true);
		}

		if (command == SHQ) {
			toc_f_shq.setVisible(true);
		}

		if (command == LTEEU) {
			sh_part1.setVisible(true);
		}

		if (command == LSO) {
			sh_part2.setVisible(true);
		}

		if (command == LSDAM) {
			sh_part3.setVisible(true);
		}

		if (command == PCTM) {
			sh_part4.setVisible(true);
		}

		if (command == TEO) {
			sh_part5.setVisible(true);
		}

		if (command == SQ) {
			System.out.println("Start Questionnaire button pushed in "
					+ frame.getName());
		}

		if (command == AR) {
			System.out.println("Analyze Results button pushed in "
					+ frame.getName());
			am.shouldProceed();
		}

		if (command == PS) {
			System.out.println("Print Summary button pushed in "
					+ frame.getName());
		}

		if (command == SAVE) {
			System.out.println("save button pushed in " + frame.getName());
			System.out.println("save button pushed in class?? "
					+ frame.getClass().getName());
			am.updateStatus();
			am.getAllComps(frame, frame);
			//getAllComps(frame,frame);
		}

		if (command == EXIT) {
			// System.out.println("exit button pushed in " + frame.getName());
			System.exit(0);
		}

		if (command == ABOUT) {
			af.setVisible(true);
		}

	}
	
	public static void getAllComps(Container parent, JFrame frame) {
		AppManager am = SMART_APP.get_am();
		System.out.println("number of components: "
				+ parent.getComponentCount());
		for (Component c : parent.getComponents()) {
			String n = c.getClass().toString();
			System.out.println("Component_NAME: " + n);

			if (c instanceof JComboBox) {
				JComboBox<?> jcb = (JComboBox<?>) c;
				System.out.println(frame.getClass().getName().replace(".", "_")
						+ "_" + jcb.getName());
				Element elem = new Element(frame.getClass().getName()
						.replace(".", "_")
						+ "_" + jcb.getName());
				String data = "Answer";
				String txt = jcb.getModel().getSelectedItem().toString();
				System.out.println("@@!! get value: " + txt);
				am.setText(elem, data, txt);
			}

			else if (c instanceof JTextArea) {
				JTextArea jta = (JTextArea) c;
				System.out.println(frame.getClass().getName().replace(".", "_")
						+ "_" + jta.getName());
				Element elem = new Element(frame.getClass().getName()
						.replace(".", "_")
						+ "_" + jta.getName());
				String data = "Text";
				String txt = jta.getText();
				am.setText(elem, data, txt);
			}

//			else if (c instanceof JCheckBox
//					&& frame.getTitle().equals(
//							"SMART-T Main Application Window")) {
//				System.out
//						.println("!!!!!!!!!!!found main window!!!!!!!!!!!!!!!!!");
//				JCheckBox jcb = (JCheckBox) c;
//				System.out.println("and the checkbox is: " + jcb.getName());
//				float bd1 = am.getPctComplete(am.getBDList());
//				System.out.println("bd1 %cmplt: " + bd1);
//				
//				if (jcb.getName().equals("bd")) {
//					if (bd1 > 99.9) {
//						System.out
//								.println("@@@@!!!!!!!bd check box!!!!!!!!!!!!!!!!!");
//						jcb.setSelected(true);
//					} else {
//						jcb.setSelected(false);
//					}
//				}
//
//				float td1 = am.getPctComplete(am.getTDList());
//				System.out.println("td1 %cmplt: " + td1);
//				if (jcb.getName().equals("td")) {
//				if (td1 > 99.9) {
//					jcb.setSelected(true);
//				} else {
//					jcb.setSelected(false);
//				}
//				}
//
//				float or1 = am.getPctComplete(am.getORList());
//				System.out.println("or1 %cmplt: " + or1);
//				if (jcb.getName().equals("or")) {
//				if (or1 > 99.9) {
//					jcb.setSelected(true);
//				} else {
//					jcb.setSelected(false);
//				}
//				}
//			}

			if (c instanceof Container) {
				getAllComps((Container) c, frame);
			}
		}
	}
	public void setChecked(String name){
	}
}
